"""\
LoginFrame is a class provided an interface to get nickname and network setting for login.

LoginFrame.__init__(self)
    Create a LoginFrame.
"""
# skin
from Shell.gui.Skin import SkinGet
# gui
from direct.gui.DirectGui import DirectFrame, DirectRadioButton, DirectLabel, DirectEntry, DirectButton, OkDialog
from pandac.PandaModules import TextNode
from Cube.gui.DirectGUIPropertiesAccess import BtnTextSet, BtnCmdSet
from Cube.gui.QSizer import ResizedCallBackSet, BoxSizer, Spacer
from Cube.gui.DirectGUIPropertiesAccess import AlignText, AlignIndicator
# uiString
from Shell.data.UIString import uiString, uiStringScale

class LoginView(DirectFrame):
    def __init__(self,
                 model,
                 parent = None,
                 ):
        """\
LoginFrame.__init__(self)
    Create a LoginFrame."""
        DirectFrame.__init__(self, parent)

        self.model = model

        ParameterDict = SkinGet("Login")
        Param_lab_Nickname = ParameterDict["lab_Nickname"]
        Param_entry_nickname = ParameterDict["entry_nickname"]
        Param_radio_ServerClient = ParameterDict["radio_ServerClient"]
        Param_entry_IP = ParameterDict["entry_IP"]
        Param_btn_LoginBck = ParameterDict["btn_LoginBck"]

        # setup a Label for host ip display
        Param_lab_Nickname["parent"] = self
        Param_lab_Nickname["text"] = "%s:"%(uiString["Nickname"])
        lab_Nickname = DirectLabel(**Param_lab_Nickname)

        # setup a Entry for nickname input
        Param_entry_nickname["parent"] = self
        Param_entry_nickname["initialText"] = self.model.Nickname
        self.entry_Nickname = DirectEntry(**Param_entry_nickname)

        # setup a pair of RadioButtons for connectType (['server'] or ['client'])
        Param_radio_ServerClient["parent"] = self
        Param_radio_ServerClient["text"] = uiString["Create a Game"]
        Param_radio_ServerClient["variable"] = self.model.ConnectType
        Param_radio_ServerClient["value"] = ['server']
        radio_Server = DirectRadioButton(**Param_radio_ServerClient)

        Param_radio_ServerClient["text"] = uiString["Join to"]
        Param_radio_ServerClient["value"] = ['client']
        radio_Client = DirectRadioButton(**Param_radio_ServerClient)
        buttons = [ radio_Server, radio_Client]
        for item in buttons: item.setOthers(buttons)

        # setup a Entry for ip address input
        Param_entry_IP["parent"] = self
        Param_entry_IP["initialText"] = self.model.HostIP
        self.entry_IP = DirectEntry(**Param_entry_IP)

        # setup a Button for login
        Param_btn_LoginBck["parent"] = self
        Param_btn_LoginBck["text"] = uiString["Login"]
        self.btn_Login = DirectButton(**Param_btn_LoginBck)
        # setup a Button for cancel (Back to Main Menu)
        Param_btn_LoginBck["text"] = uiString["Back"]
        self.btn_Back = DirectButton(**Param_btn_LoginBck)


        sizerName = BoxSizer(BoxSizer.HORIZONTAL, gap = 0.01)
        sizerName.Add(lab_Nickname, 1)
        sizerName.Add(self.entry_Nickname, 1)

        sizerCreateRoom = BoxSizer(BoxSizer.HORIZONTAL, gap = 0.02)
        sizerCreateRoom.Add(radio_Server, 1)
        sizerCreateRoom.Add(BoxSizer(BoxSizer.VERTICAL), 1)

        sizerJoinRoom = BoxSizer(BoxSizer.HORIZONTAL, gap = 0.02)
        sizerJoinRoom.Add(radio_Client, 1)
        sizerJoinRoom.Add(self.entry_IP, 1)

        sizerBtn = BoxSizer(BoxSizer.HORIZONTAL, gap = 0.02)
        sizerBtn.Add(self.btn_Back, 1)
        sizerBtn.Add(self.btn_Login, 1)

        sizerMain = BoxSizer(BoxSizer.VERTICAL, gap = 0.05, size = (-0.6,0.6,-0.4,0.4))
        sizerMain.Add(sizerName,1)
        sizerMain.Add(sizerCreateRoom,1)
        sizerMain.Add(sizerJoinRoom,1)
        sizerMain.Add(Spacer(0.1),0)
        sizerMain.Add(sizerBtn,1)

        # alignment gui object's text
        ResizedCallBackSet(lab_Nickname, AlignText, frame = lab_Nickname, align = "" )
        ResizedCallBackSet(self.entry_Nickname, AlignText, frame = self.entry_Nickname, align = "left" )
        ResizedCallBackSet(radio_Server, AlignText, frame = radio_Server, align = "" )
        ResizedCallBackSet(radio_Client, AlignText, frame = radio_Client, align = "" )
        ResizedCallBackSet(self.entry_IP, AlignText, frame = self.entry_IP, align = "left" )
        ResizedCallBackSet(self.btn_Login, AlignText, frame = self.btn_Login, align = "" )
        ResizedCallBackSet(self.btn_Back, AlignText, frame = self.btn_Back, align = "" )
        # do sizer layout
        sizerMain.Layout()

        # set properties of indicator in DirectRadioButton
        text_scale = Param_radio_ServerClient["text_scale"]
        for item in buttons: AlignIndicator(item,text_scale)

    def ControllerSet(self, controller):
        self.controller = controller
        BtnCmdSet(self.entry_Nickname, controller.NickNameSetHandle)
        BtnCmdSet(self.entry_IP, controller.IPAddressSetHandle)
        BtnCmdSet(self.btn_Login, controller.LoginHandle)
        BtnCmdSet(self.btn_Back, controller.BackToMainMenuHandle)


# login fail messages ---------------------------------------------------------------
    def LoginFailDisplay(self, uiString, fn):
        self.ErrorDisplayDialog = OkDialog(text = uiString,
                                           command = fn)

    def ErrorDisplayDialogDestroy(self):
        self.ErrorDisplayDialog.destroy()
